Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ESM data uri import and mock #12392

Merged
merged 12 commits into from
Feb 16, 2022
Merged

feat: ESM data uri import and mock #12392

merged 12 commits into from
Feb 16, 2022

Conversation

tbossi
Copy link
Contributor

@tbossi tbossi commented Feb 15, 2022

Summary

This change should close #12370
Allows dynamic import (and mock) from data: URI as it works on node.js:

const code = 'export const foo = 123'
const imported = await import(`data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`)
// OR
const imported = await import(`data:text/javascript;base64,${btoa(code)}`)
// imported.foo === 123

Test plan

Added e2e tests in native-esm.test.js to check:

  • import when code is URI component encoded;
  • import when code is Base64 encoded;
  • further imports of the same data URI are cached;
  • data URI can be mocked with jest.unstable_mockModule().

@facebook-github-bot
Copy link
Contributor

Hi @tbossi!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome work, thank you so much!

Can you add some tests for static imports (not just dynamic), plus cases where it fails due to malformed scheme or data?

We also need a changelog entry 🙂

e2e/native-esm/__tests__/native-esm.test.js Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay, this is cool 👍

Comment on lines 597 to 599
if (mime === 'application/json') {
code = 'export default ' + code;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should use SyntheticModule rather than SourceTextModule

packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
}

const match = specifier.match(
/^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we lift this regex to the module level as a constant?

e2e/native-esm/staticDataImport.js Show resolved Hide resolved
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@codecov-commenter
Copy link

Codecov Report

Merging #12392 (67f7e63) into main (4590b9d) will decrease coverage by 0.14%.
The diff coverage is 2.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #12392      +/-   ##
==========================================
- Coverage   68.48%   68.33%   -0.15%     
==========================================
  Files         325      325              
  Lines       16971    17009      +38     
  Branches     5061     5072      +11     
==========================================
+ Hits        11622    11623       +1     
- Misses       5317     5354      +37     
  Partials       32       32              
Impacted Files Coverage Δ
packages/jest-resolve/src/resolver.ts 55.55% <0.00%> (-0.52%) ⬇️
packages/jest-runtime/src/index.ts 53.31% <2.77%> (-2.10%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4590b9d...67f7e63. Read the comment docs.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great, thanks!

@SimenB SimenB changed the title ESM data uri import and mock feat: ESM data uri import and mock Feb 16, 2022
@SimenB SimenB merged commit adf8526 into jestjs:main Feb 16, 2022
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: dynamic import from 'data:' URI
4 participants